<template>
  <a @click="downloadFile" class="custom-file" alt="" srcset="">{{ name }}</a>
</template>

<script setup lang="ts">
import { getBlobDownloadFile } from "@/api/module/commone";

const props = defineProps(["data"]);

const name = props.data.split("|")[0] as string;
const code = props.data.split("|")[1] as string;

const downloadFile = async () => {
  const fileName = name;
  const blob = await getBlobDownloadFile(code);

  let downloadElement = document.createElement("a");
  let href = blob;
  if (typeof blob == "string") {
    downloadElement.target = "_blank";
  } else {
    href = window.URL.createObjectURL(blob); //创建下载的链接
  }
  downloadElement.href = href;
  downloadElement.download = fileName; //下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); //触发点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
  if (typeof blob != "string") {
    window.URL.revokeObjectURL(href); //释放掉blob对象
  }
};
</script>
<style scoped lang="less">
.custom-file {
  cursor: pointer;
  color: #1e9fff;
  margin-left: 10px;
  &:first-child {
    margin-left: 0;
  }
}
</style>
